package ex.datastream.functions.function;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

import java.util.Random;

/**
 * 按逗号拆分数据
 */
public class FlatMapFuncBySplitter02 implements FlatMapFunction<String, Tuple2<String, Integer>> {
    @Override
    public void flatMap(String sentence, Collector<Tuple2<String, Integer>> out) throws Exception {
        try {
//            String []str=sentence.split(",");
//            out.collect(new Tuple2<String, Integer>(str[0], Integer.parseInt(str[1])));
            String[] words = sentence.split(",");
            for (String word : words) {
                Random random = new Random();
                int i = random.nextInt(5);
                if (i > 3) {
                    System.out.println("出bug了...");
                    throw new RuntimeException("出bug了...");
                }
                out.collect(Tuple2.of(word, 1));
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}